home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / rdist-6.1 / rdist-6 / rdist-6.1.0-linuxpl2 / src / filesys-os.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-31  |  8.6 KB  |  401 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char RCSid[] = 
  36. "$Id: filesys-os.c,v 6.15 1994/03/31 04:01:44 mcooper Exp $";
  37.  
  38. static char sccsid[] = "@(#)filesys-os.c";
  39.  
  40. static char copyright[] =
  41. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  42.  All rights reserved.\n";
  43. #endif /* not lint */
  44.  
  45. /*
  46.  * OS specific file system routines
  47.  */
  48.  
  49. #include "defs.h"
  50. #include "filesys.h"
  51.  
  52. #if     FSI_TYPE == FSI_GETFSSTAT
  53. static struct statfs   *mnt = NULL;
  54. typedef u_long         ulong;
  55. #endif    /* FSI_GETFSSTAT */
  56.  
  57. #if    FSI_TYPE == FSI_MNTCTL
  58. static struct vmount   *mnt = NULL;
  59. #endif    /* FSI_MNTCTL */
  60.  
  61. #if    (FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
  62. static char            *mntbuf = NULL;
  63. static int         entries_left;
  64. #endif    /* FSI_MNTCTL || FSI_GETFSSTAT */
  65.  
  66. #if    FSI_TYPE == FSI_MNTCTL
  67. /*
  68.  * AIX version of setmountent()
  69.  */
  70. FILE *setmountent(file, mode)
  71.     /*ARGSUSED*/
  72.     char *file;
  73.     char *mode;
  74. {
  75.     ulong size;
  76.  
  77.     if (mntbuf)
  78.         (void) free(mntbuf);
  79.  
  80.     mntctl(MCTL_QUERY, sizeof(size), &size);
  81.     mntbuf = (char *) xmalloc(size);
  82.  
  83.     entries_left = mntctl(MCTL_QUERY, size, mntbuf);
  84.     if (!entries_left)
  85.         return((FILE *)NULL);
  86.  
  87.     mnt = (struct vmount *)mntbuf;
  88.     return((FILE *) 1);
  89. }
  90. #endif    /* FSI_MNTCTL */
  91.  
  92. #if    FSI_TYPE == FSI_GETFSSTAT
  93. /*
  94.  * getfsstat() version of get mount info routines.
  95.  */
  96. FILE *setmountent(file, mode)
  97.     /*ARGSUSED*/
  98.     char *file;
  99.     char *mode;
  100. {
  101.     ulong size;
  102.  
  103.     if (mntbuf)
  104.         (void) free(mntbuf);
  105.  
  106.     size = getfsstat((struct statfs *) NULL, 0, MNT_WAIT);
  107.     size *= sizeof(struct statfs);
  108.     mntbuf = (char *) xmalloc(size);
  109.  
  110.     entries_left = getfsstat((struct statfs *)mntbuf, size, MNT_WAIT);
  111.     if (entries_left == -1)
  112.         return((FILE *) NULL);
  113.  
  114.     mnt = (struct statfs *) mntbuf;
  115.  
  116.     return((FILE *) 1);
  117. }
  118. #endif    /* FSI_GETFSSTAT */
  119.  
  120. #if    FSI_TYPE == FSI_MNTCTL
  121. /* 
  122.  * AIX version of getmountent() 
  123.  */
  124. /*
  125.  * Iterate over mount entries
  126.  */
  127. mntent_t *getmountent(fptr)
  128.     /*ARGSUSED*/
  129.     FILE *fptr;
  130. {
  131.     static mntent_t mntstruct;
  132.  
  133.     if (!entries_left)
  134.         return((mntent_t*)0);
  135.  
  136.     bzero((char *) &mntstruct, sizeof(mntstruct));
  137.  
  138.     if (mnt->vmt_flags & MNT_READONLY)
  139.         mntstruct.me_flags |= MEFLAG_READONLY;
  140.  
  141.     mntstruct.me_path = vmt2dataptr(mnt, VMT_STUB);
  142.     switch ((ulong)(struct vmount*)mnt->vmt_gfstype) {
  143.           case MNT_NFS:
  144.         mntstruct.me_type = METYPE_NFS;
  145.         break;
  146.           default:
  147.         mntstruct.me_type = METYPE_OTHER;
  148.         break;
  149.     }
  150.  
  151.     mnt = (struct vmount*)((mnt->vmt_length)+(ulong)mnt);
  152.     entries_left--;
  153.  
  154.     return(&mntstruct);
  155. }
  156. #endif    /* FSI_MNTCTL */
  157.  
  158. #if    FSI_TYPE == FSI_GETFSSTAT
  159. /*
  160.  * getfsstat() version of getmountent()
  161.  */
  162. mntent_t *getmountent(fptr)
  163.     /*ARGSUSED*/
  164.     FILE *fptr;
  165. {
  166.     static mntent_t mntstruct;
  167.     static char remote_dev[MAXHOSTNAMELEN+MAXPATHLEN+1];
  168.  
  169.     if (!entries_left)
  170.         return((mntent_t*)0);
  171.  
  172.     bzero((char *) &mntstruct, sizeof(mntstruct));
  173.  
  174. #if    defined(MNT_RDONLY)
  175.     if (mnt->f_flags & MNT_RDONLY)
  176.         mntstruct.me_flags |= MEFLAG_READONLY;
  177. #endif
  178. #if    defined(M_RDONLY)
  179.     if (mnt->f_flags & M_RDONLY)
  180.         mntstruct.me_flags |= MEFLAG_READONLY;
  181. #endif
  182.  
  183.     switch ((ulong)(struct statfs*)mnt->f_type) {
  184.           case MOUNT_NFS:
  185.         (void) sprintf(remote_dev, "%s", mnt->f_mntfromname);
  186.         mntstruct.me_path = remote_dev;
  187.         mntstruct.me_type = METYPE_NFS;
  188.         break;
  189.           default:
  190.         mntstruct.me_path = mnt->f_mntonname;
  191.         mntstruct.me_type = METYPE_OTHER;
  192.         break;
  193.     }
  194.  
  195.     mnt = (struct statfs*)(sizeof(struct statfs)+(ulong)mnt);
  196.     entries_left--;
  197.  
  198.     return(&mntstruct);
  199. }
  200. #endif
  201.  
  202. #if    (FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
  203. /*
  204.  * Done with iterations
  205.  */
  206. void endmountent(fptr)
  207.     /*ARGSUSED*/
  208.     FILE *fptr;
  209. {
  210.     mnt = NULL;
  211.  
  212.     if (mntbuf) {
  213.         (void) free(mntbuf);
  214.         mntbuf = (char *) NULL;
  215.     }
  216. }
  217. #endif    /* FSI_MNTCTL || FSI_GETFSSTAT */
  218.  
  219. #if    FSI_TYPE == FSI_GETMNTENT2
  220. /*
  221.  * Prepare to iterate over mounted filesystem list
  222.  */
  223. FILE *setmountent(file, mode)
  224.     /*ARGSUSED*/
  225.     char *file;
  226.     char *mode;
  227. {
  228.     return(fopen(file, mode));
  229. }
  230.  
  231. /*
  232.  * Done with iteration
  233.  */
  234. void endmountent(fptr)
  235.     /*ARGSUSED*/
  236.     FILE *fptr;
  237. {
  238.     fclose(fptr);
  239. }
  240.  
  241. /*
  242.  * Iterate over mount entries
  243.  */
  244. mntent_t *getmountent(fptr)
  245.     FILE *fptr;
  246. {
  247.     static mntent_t me;
  248.     static struct mnttab mntent;
  249.  
  250.     bzero((char *)&me, sizeof(mntent_t));
  251.  
  252. #if     defined(UNICOS)
  253.         if (getmntent(fptr, &mntent) != NULL) {
  254. #else
  255.         if (getmntent(fptr, &mntent) != -1) {
  256. #endif
  257.         me.me_path = mntent.mnt_mountp;
  258.         me.me_type = mntent.mnt_fstype;
  259.         if (mntent.mnt_mntopts && hasmntopt(&mntent, MNTOPT_RO))
  260.             me.me_flags |= MEFLAG_READONLY;
  261.  
  262. #if    defined(MNTTYPE_IGNORE)
  263.         if (strcmp(mntent.mnt_fstype, MNTTYPE_IGNORE) == 0)
  264.             me.me_flags |= MEFLAG_IGNORE;
  265. #endif    /* MNTTYPE_IGNORE */
  266. #if    defined(MNTTYPE_SWAP)
  267.         if (strcmp(mntent.mnt_fstype, MNTTYPE_SWAP) == 0)
  268.             me.me_flags |= MEFLAG_IGNORE;
  269. #endif    /* MNTTYPE_SWAP */
  270.  
  271.         return(&me);
  272.     } else
  273.         return((mntent_t *) NULL);
  274. }
  275. #endif    /* FSI_GETMNTNET2 */
  276.  
  277. #if    FSI_TYPE == FSI_GETMNTENT
  278. /*
  279.  * Prepare to iterate over mounted filesystem list
  280.  */
  281. FILE *setmountent(file, mode)
  282.     /*ARGSUSED*/
  283.     char *file;
  284.     char *mode;
  285. {
  286.     return(setmntent(file, mode));
  287. }
  288.  
  289. /*
  290.  * Done with iteration
  291.  */
  292. void endmountent(fptr)
  293.     /*ARGSUSED*/
  294.     FILE *fptr;
  295. {
  296.     endmntent(fptr);
  297. }
  298.  
  299. /*
  300.  * Iterate over mount entries
  301.  */
  302. mntent_t *getmountent(fptr)
  303.     FILE *fptr;
  304. {
  305.     static mntent_t me;
  306.     struct mntent *mntent;
  307.  
  308.     bzero((char *)&me, sizeof(mntent_t));
  309.  
  310.     if (mntent = getmntent(fptr)) {
  311.         me.me_path = mntent->mnt_dir;
  312.         me.me_type = mntent->mnt_type;
  313.         if (mntent->mnt_opts && hasmntopt(mntent, MNTOPT_RO))
  314.             me.me_flags |= MEFLAG_READONLY;
  315.  
  316. #if    defined(MNTTYPE_IGNORE)
  317.         if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
  318.             me.me_flags |= MEFLAG_IGNORE;
  319. #endif    /* MNTTYPE_IGNORE */
  320. #if    defined(MNTTYPE_SWAP)
  321.         if (strcmp(mntent->mnt_type, MNTTYPE_SWAP) == 0)
  322.             me.me_flags |= MEFLAG_IGNORE;
  323. #endif    /* MNTTYPE_SWAP */
  324.  
  325.         return(&me);
  326.     } else
  327.         return((mntent_t *) NULL);
  328. }
  329. #endif    /* FSI_GETMNTNET */
  330.  
  331. #if    FSI_TYPE == FSI_GETMNT
  332. /*
  333.  * getmnt() interface (Ultrix)
  334.  */
  335.  
  336. #include <sys/fs_types.h>
  337.  
  338. static int startmounts = 0;
  339.  
  340. FILE *setmountent(file, mode)
  341.     /*ARGSUSED*/
  342.     char *file;
  343.     char *mode;
  344. {
  345.     startmounts = 0;
  346. }
  347.  
  348. void endmountent(fptr)
  349.     /*ARGSUSED*/
  350.     FILE *fptr;
  351. {
  352.     /* NOOP */
  353. }
  354.  
  355. /*
  356.  * Iterate over mounted filesystems using getmnt()
  357.  */
  358. mntent_t *getmountent(fptr)
  359.     /*ARGSUSED*/
  360.     FILE *fptr;
  361. {
  362.     struct fs_data fs_data;
  363.     static mntent_t me;
  364.  
  365.     if (getmnt(&startmounts, &fs_data, sizeof(fs_data), NOSTAT_MANY, 
  366.            (char *) NULL) <= 0)
  367.         return((mntent_t *) NULL);
  368.  
  369.     bzero((char *)&me, sizeof(mntent_t));
  370.     me.me_path = fs_data.fd_path;
  371.     if (fs_data.fd_fstype == GT_NFS)
  372.         me.me_type = METYPE_NFS;
  373.     else
  374.         me.me_type = METYPE_OTHER;
  375.  
  376.     if (fs_data.fd_flags & M_RONLY)
  377.         me.me_flags |= MEFLAG_READONLY;
  378.  
  379.     return(&me);
  380. }
  381. #endif    /* FSI_GETMNT */
  382.  
  383. /*
  384.  * Make a new (copy) of a mntent structure.
  385.  */
  386. mntent_t *newmountent(old)
  387.     mntent_t *old;
  388. {
  389.     mntent_t *new;
  390.  
  391.     if (!old)
  392.         return((mntent_t *) NULL);
  393.  
  394.     new = (mntent_t *) xcalloc(1, sizeof(mntent_t));
  395.     new->me_path = strdup(old->me_path);
  396.     new->me_type = strdup(old->me_type);
  397.     new->me_flags = old->me_flags;
  398.  
  399.     return(new);
  400. }
  401.